package com.dhcc.cdc.sys.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.sys.entity.DictypeEntity;
import com.dhcc.cdc.sys.service.IDictypeService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 系统字典类型
 * 
 * @ClassName: DictypeController
 * @Description: TODO
 * @author jumper
 * @date 2021-04-16 17:11:56
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/dictype")
public class DictypeController extends BaseController {

	private String PREFIX = "/backend/cdc/dictype/";
    //产品线代码
    private String productCode;

	@Autowired
	private IDictypeService dictypeService;
	

    /**
     * 跳转到系统字典类型列表首页
     */
    @RequestMapping("/index/{productCode}")
    @Permission("dictype:list")
    public String index(@PathVariable String productCode, Model model) {
        if("all".equals(productCode)) productCode=null;
        this.productCode = productCode;
        model.addAttribute("productCode",productCode);
        return PREFIX + "dictype.html";
    }
	
    /**
     * 跳转到新增系统字典类型
     */
    @RequestMapping("/to_add")
    @Permission("dictype:add")
    public String toAdd(Model model) {
        model.addAttribute("productCode",this.productCode);
        return PREFIX + "dictype_add.html";
    }
	
	/**
     * 跳转到修改系统字典类型
     */
    @RequestMapping("/to_update/{dictypeId}")
    @Permission("dictype:update")
    public String toUpdate(@PathVariable Long dictypeId, Model model) {
        DictypeEntity dictype = this.dictypeService.selectById(dictypeId);
        model.addAttribute("dictype",dictype);
        model.addAttribute("productCode",this.productCode);
        return PREFIX + "dictype_edit.html";
    }

    /**
     * 获取系统字典类型列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("dictype:list")
    public Object list(@RequestParam Map<String, Object> params) {
        if (this.productCode != null) {
            params.put("productCode", this.productCode);
        }
		Page<DictypeEntity> page = new PageFactory<DictypeEntity>().defaultPage();
        List<DictypeEntity> result = this.dictypeService.page(page,params);
        page.setRecords((List<DictypeEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    @RequestMapping(value = "/query")
    @Permission("dictype:list")
    @ResponseBody
    public Object query(@RequestParam Map<String, Object> params) {
        List<DictypeEntity> result = this.dictypeService.list(params);
    	return result;
    }
    /**
     * 新增系统字典类型
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("dictype:add")
    @BussinessLog(value = "新增系统字典类型")
    public Object add(DictypeEntity dictype) {
        if (CommonUtil.isOneEmpty(dictype)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        dictype.insert();
        return R.ok();
    }

    /**
     * 删除系统字典类型
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("dictype:delete")
    @BussinessLog(value = "删除系统字典类型")
    public Object delete(@RequestParam Long dictypeId) {
	
        this.dictypeService.deleteById(dictypeId);

        return R.ok();
    }

    /**
     * 修改系统字典类型
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("dictype:update")
    @BussinessLog(value = "修改系统字典类型")
    public Object update(DictypeEntity dictype) {
        if (CommonUtil.isOneEmpty(dictype)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        DictypeEntity old = this.dictypeService.selectById(dictype.getId());

        old.setCode(dictype.getCode());
        old.setDescription(dictype.getDescription());
        old.setProductId(dictype.getProductId());
        old.setIsActive(dictype.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
